// pages/homepage/popup/popup.js
var app = getApp();
var DateUtil = require('../../../utils/DateUtil.js');
Component({
  /**
   * 组件的属性列表
   */
  properties: {

  },

  /**
   * 组件的初始数据
   */
  data: {
    showRegister: false,
  },

  attached: function() {
    this.registerAwards();
  },

  /**
   * 组件的方法列表
   */
  methods: {
    /**
     * 获取注册奖励
     */
    registerAwards: function() {
      var hasRegister = wx.getStorageSync('hasRegister') || false;
      //已获取注册奖励，去拿活动商品信息
      if (hasRegister) {
        this.getActitivyGoods();
        return;
      }

      app.Ajax({
        method: 'POST',
        url: '/api/index.php?app=home&act=awards',
        success: (res) => {
          this.setData({
            showRegister: true,
          })
          wx.setStorageSync('hasRegister', true)
        },
        fail: (err, code) => {
          if (code) {
            console.log(err);
            if (code == 4) {
              wx.setStorageSync('hasRegister', true)
            }
          }
          //去拿活动商品信息
          this.getActitivyGoods();
        }
      })
    },

    /**
     * 关闭注册奖励页面
     */
    registerClose: function() {
      this.setData({
        showRegister: false,
      })
      //去拿活动商品信息
      this.getActitivyGoods();
    },

    /**
     * 获取活动商品
     */
    getActitivyGoods: function() {
      //一天只展示一次
      var now = parseInt(new Date().getTime() / 1000);
      var today = DateUtil.formatTimestamp(now, 'yyyy-MM-dd');
      var goodsShowDay = wx.getStorageSync('goodsShowDay');
      if (today == goodsShowDay) {
        return;
      }

      app.Ajax({
        url: '/api/index.php?app=group&act=popup',
        success: (res) => {
          if (res.is_show_spell_popup == true) {
            this.setData({
              showActivityGoods: true,
              goods: res
            })
            wx.setStorageSync('goodsShowDay', today)
          }
        }
      })
    },

    /**
     * 跳转到开团页面
     */
    goGroupExchagePage: function() {
      this.activityGoodsClose();
      wx.navigateTo({
        url: '../../pages/activity/group_exchange/main_page/main_page?group_id=' + this.data.goods.group_id,
      })
    },

    /**
     * 关闭拼团页面
     */
    activityGoodsClose: function() {
      this.setData({
        showActivityGoods: false,
      })
    }
  }
})